package com.hxzy.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.R;
import com.hxzy.entity.AddressBook;
import com.hxzy.service.AddressBookService;
import com.hxzy.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "前端收货地址API")
@RestController
@RequestMapping(value = "/app/addressBook")
public class FrontAddressBookController extends BaseController {

    @Autowired
    private AddressBookService  addressBookService;


    @ApiOperation(value = "获取当前用户所有收货地址")
    @GetMapping(value = "/data")
    public R addressList(){
        LoginVO  loginVO= WaimaiThreadLocal.getCurrent();

        //mybatis-plus查询
        QueryWrapper<AddressBook>  queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", loginVO.getAccountVO().getId());
        queryWrapper.eq("is_deleted",0);
        queryWrapper.orderByDesc("is_default");

        List<AddressBook> addressBookList = this.addressBookService.list(queryWrapper);
        return R.okHasData(addressBookList);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R findById(@PathVariable(value = "id") Long id){
        AddressBook addressBook = this.addressBookService.getById(id);
        if(addressBook==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(addressBook);
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Valid AddressBook  addressBook){
       LoginVO  loginVO=WaimaiThreadLocal.getCurrent();
       addressBook.setUserId(loginVO.getAccountVO().getId());

        boolean result = this.addressBookService.save(addressBook);
        return super.toR(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public R edit(@RequestBody @Valid AddressBook  addressBook){
        AddressBook db = this.addressBookService.getById(addressBook.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        //复制值
        BeanUtils.copyProperties(addressBook,db,"userId");

        if(db.getIsDefault()) {
            //查询修改时候，判断当前默认收货地址有几个，最多只允许出现一个
            QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", db.getUserId());
            queryWrapper.eq("is_default", 1);
            queryWrapper.ne("id", db.getId());
            long count = this.addressBookService.count(queryWrapper);
            if(count>0){
                db.setIsDefault(false);
            }
        }

        boolean result = this.addressBookService.updateById(addressBook);
        return super.toR(result);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    public R delete(@PathVariable(value = "id") Long id){
        int count=this.addressBookService.delete(id);
        return super.toR(count);
    }


    @ApiOperation(value = "设定为默认收货人")
    @PostMapping(value = "/default/{id}")
    public R updateDefault(@PathVariable(value = "id") Long id){
        int count=this.addressBookService.updateDefault(id);
        return super.toR(count);
    }

    @ApiOperation(value = "获取到默认的收货地址")
    @GetMapping(value = "/default")
    public R defaultAddress(){
        LoginVO  loginVO= WaimaiThreadLocal.getCurrent();

        QueryWrapper<AddressBook> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_default",1);
        queryWrapper.eq("user_id", loginVO.getAccountVO().getId());

        AddressBook dbAddress = this.addressBookService.getOne(queryWrapper);

        if(dbAddress==null){
            return R.build(AckCode.NO_DEFAULT_ADDRESS);
        }

        return R.okHasData(dbAddress);
    }
}
